package com.hsnn.medstgmini.base.dict.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.service.DicDrugnameLogManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 通用名字典维护记录
 * @author 郦东飞
 * @date 2015年11月11日
 */
@Controller
@RequestMapping("/dicDrugnameLog")
public class DicDrugnameLogController {
	
	private static final Logger log = Logger.getLogger(DicDrugnameController.class);
			
	
	@Autowired
	private DicDrugnameLogManager dicDrugnameLogManager;

	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 
	 *@category 跳转通用名字典维护记录页面
	 *@author lidongfei
	 * @throws UnsupportedEncodingException 
	 *@date 2015年11月11日
	 *String
	 */
	@RequestMapping("/log")
	public String  toDicDrugnameLogList() throws UnsupportedEncodingException {
		printParams();
		request.setAttribute("drugnameCode", request.getParameter("drugnameCode"));
		String drugName = new String(request.getParameter("drugName").getBytes("ISO8859-1"),"utf-8");
		request.setAttribute("drugName", drugName);
		return "/base/dict/drugnameLogList";
	}
	
	/**
	 * 
	 *@category 获取通用名字典维护记录列表
	 *@author lidongfei
	 *@date 2015年11月11日
	 *String
	 */
	@RequestMapping("/getDicDrugnameLogList")
	@ResponseBody
	public Pagination  getDrgnameLogList(){
		printParams();
		Pagination  page = new Pagination(request);
		page.getConditions().put("startTime", request.getParameter("date1"));
		page.getConditions().put("endTime", request.getParameter("date2"));
		page.getConditions().put("drugnameCode", request.getParameter("drugnameCode"));
		try {
			dicDrugnameLogManager.getDicDrugnameLogList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 跳转通用名字典维护记录详情页面
	 *@author 郦东飞
	 *@date 2015年11月11日
	 *String
	 */
	@RequestMapping("/toDicDrugnameLogDetail")
	public String  toDicDrugnameLogDetail() {
		printParams();
		try {
			Pagination  page = new Pagination();
			page.getConditions().put("logId", request.getParameter("logId"));
			List<Map<String,Object>> list = dicDrugnameLogManager.getDicDrugnameLogDetail(page);
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"通用名编码","通用名名称","状态"};
			String[] eColunm = {"drugnameCode","drugName","status"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("dataList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*log.info("enter toDicDrugnameLogDetail method!");
		Pagination  page = new Pagination();
		page.getConditions().put("logId", request.getParameter("logId"));
		List<Map<String,Object>> list = dicDrugnameLogManager.getDicDrugnameLogDetail(page);
		request.setAttribute("list", list);*/
		return "/base/dict/drugnameLogDetail";
	}

	/**
	 * 
	 *@category 获取参数打印
	 *@author 郦东飞
	 *@date 2015年11月11日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
}
